package com.weiiew.common.OnlyFeign;
import com.weiiew.common.exception.OnlyFeignException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class OnlyFeignAspect {

    // 切入带有GatewayAccess注解的方法或者类
    @Around("@within(onlyFeign) || @annotation(onlyFeign)")
    public Object checkAccess(ProceedingJoinPoint joinPoint, OnlyFeign onlyFeign) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .currentRequestAttributes()).getRequest();
        // 从请求头获取网关类型
        String gatewayHeader = request.getHeader("X-Gateway-Type");
//        System.out.println(gatewayHeader);
        if (gatewayHeader != null) throw new OnlyFeignException("内部Feign专用接口，外部不可访问");
        return joinPoint.proceed();
    }
}